package com.sskj.qiangjin.ui.roll;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import java.lang.ref.WeakReference;

/**
 * Created by Q-Kang on 2019/1/5.
 */

public class AutoPollRecyclerView extends RecyclerView {

    private static final long TIME_AUTO_POLL = 16;
    AutoPollTask autoPollTask;
    AutoPollStopTask autoPollStopTask;
    private boolean running; //表示是否正在自动轮询
    private boolean canRun;//表示是否可以自动轮询

    private float startSpeed = 0.1f;
    private float stopSpeed = 30;

    public AutoPollRecyclerView(Context context) {
        super(context);
        autoPollTask = new AutoPollTask(this);
        autoPollStopTask = new AutoPollStopTask(this);
    }

    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        autoPollTask = new AutoPollTask(this);
        autoPollStopTask = new AutoPollStopTask(this);
    }

    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        autoPollTask = new AutoPollTask(this);
        autoPollStopTask = new AutoPollStopTask(this);
    }

    class AutoPollTask implements Runnable {
        private final WeakReference<AutoPollRecyclerView> mReference;


        //使用弱引用持有外部类引用->防止内存泄漏
        public AutoPollTask(AutoPollRecyclerView reference) {
            this.mReference = new WeakReference<AutoPollRecyclerView>(reference);
        }

        @Override
        public void run() {
            AutoPollRecyclerView recyclerView = mReference.get();
            if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
                recyclerView.scrollBy(2, (int) startSpeed);
                if (startSpeed <= 30) {
                    startSpeed += 0.1;
                }
                recyclerView.postDelayed(recyclerView.autoPollTask, TIME_AUTO_POLL);
            }
        }
    }

    class AutoPollStopTask implements Runnable {
        private final WeakReference<AutoPollRecyclerView> mReference;


        //使用弱引用持有外部类引用->防止内存泄漏
        public AutoPollStopTask(AutoPollRecyclerView reference) {
            this.mReference = new WeakReference<AutoPollRecyclerView>(reference);
        }

        @Override
        public void run() {
            AutoPollRecyclerView recyclerView = mReference.get();
            if (recyclerView != null && !recyclerView.running && recyclerView.canRun) {
                recyclerView.scrollBy(2, (int) stopSpeed);

                if (stopSpeed > 4.5) {
                    stopSpeed -= 0.1;
                    recyclerView.postDelayed(recyclerView.autoPollStopTask, TIME_AUTO_POLL);
                } else {
                    runningStop.onStop();
                }
            }
        }
    }

    //开启:如果正在运行,先停止->再开启
    public void start() {
        if (running)
            stop();
        canRun = true;
        running = true;
        startSpeed = 0.1f;
        postDelayed(autoPollTask, TIME_AUTO_POLL);
    }

    public void stop() {
        running = false;
        stopSpeed = 30;
        postDelayed(autoPollStopTask, TIME_AUTO_POLL);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
//        switch (e.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                if (running)
//                    stop();
//                break;
//            case MotionEvent.ACTION_UP:
//            case MotionEvent.ACTION_CANCEL:
//            case MotionEvent.ACTION_OUTSIDE:
//                if (canRun)
//                    start();
//                break;
//        }
        return false;
    }


    private RunningStop runningStop;

    public void setRunningStop(RunningStop runningStop) {
        this.runningStop = runningStop;
    }

    public interface RunningStop {
        void onStop();
    }


}
